$(document).ready(function () {
    for(var i = 2020; i >= 1920; i--) {
        $("select[name='year']").append("<option value='"+i+"'>"+i+"</option>");
    }

    for(var i = 12; i >= 1; i--) {
        $("select[name='month']").append("<option value='"+(i<10?"0":"")+i+"'>"+(i<10?"0":"")+i+"</option>");
    }

    for(var i = 31; i >= 1; i--) {
        $("select[name='day']").append("<option value='"+(i<10?"0":"")+i+"'>"+(i<10?"0":"")+i+"</option>");
    }

    $("span[name='logout']").click(function (e) { 
        e.preventDefault();
        window.location.href="login.html"
    });

    $("button[id='submit']").click(function (e) {
        var name = $("input[name='name']").val();
        var sex = $("#man").attr("myselected");

        var year = $("select[name='year']").val();
        var month = $("select[name='month']").val();
        var day = $("select[name='day']").val();

        var phoneNumber = $("input[name='phoneNumber']").val();
        var address = $("input[name='address']").val();
        var vocation = $("input[name='vocation']").val();

        var medicalHistory = $("textarea[name='medicalHistory']").val();
        var drugAllergy = $("textarea[name='drugAllergy']").val();

        var username = getQueryVariable('username')

        // 装配为后端需要的内容
        sex = (sex == 'true')
        console.log(sex)

        var user = {
            username:username,
            name:name,
            sex:sex,
            dateStr:year+'-'+month+'-'+day,
            phoneNumber:phoneNumber,
            address:address,
            vocation:vocation,
            medicalHistory:medicalHistory,
            drugAllergy:drugAllergy
        }
        user = JSON.stringify(user)

        console.log(user)

        $.ajax({
            type: "POST",
            url: "user/updateUserInfo",
            data: user,
            contentType: "application/json",
            success: function (res) {
                console.log(res)
                if(res) {
                    alert('个人信息提交成功')
                }
                else {
                    alert('个人信息提交失败，请检查数据合法性')
                }
            }
        });
    })

    man()
    init()
});

function man() {
    console.log('man')
    $("#man").attr("myselected", "true");
    $("#woman").attr("myselected", "false");
}

function woman() {
    console.log('woman')
    $("#man").attr("myselected", "false");
    $("#woman").attr("myselected", "true");
}

function init() {
    var username = getQueryVariable('username')

    $("#man").click(function (e) { 
        man()
    });

    $("#woman").click(function (e) { 
        woman()
    });

    $.ajax({
        type: "POST",
        url: "user/getUserInfo",
        data: "username="+username,
        success: function (res) {
            var name = res.name
            var sex = res.sex

            var date = res.dateStr.split('-')
            var year = date[0]
            var month = date[1]
            var day = date[2]

            var phoneNumber = res.phoneNumber
            var address = res.address
            var vocation = res.vocation

            var medicalHistory = res.medicalHistory
            var drugAllergy = res.drugAllergy

            console.log(res)
            console.log(name,sex,date)

            $("input[name='name']").val(name);
            $("input[name='sex']").val(sex?'man':'woman');
            if(!sex) $("#woman").prop("checked", "checked");
            $("select[name='year']").val(year);
            $("select[name='month']").val(month);
            $("select[name='day']").val(day);
            $("input[name='phoneNumber']").val(phoneNumber);
            $("input[name='address']").val(address);
            $("input[name='vocation']").val(vocation);
            $("textarea[name='medicalHistory']").val(medicalHistory);
            $("textarea[name='drugAllergy']").val(drugAllergy);

            if(sex) man()
            else woman()
        }
    });
}

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}